<?php
/**
 * Created by JetBrains PhpStorm.
 * File: page.class.php
 * User: Xnfy520@gmail.com
 * Date: 12-3-7
 * Time: 下午2:55
 */
	class page{

		private $total;
		private $num;
		private $cpage;
		private $tpage;
		private $limit;
		private $uri;
		private $config;
		private $fnum;
		private $limitfirst;

		function __construct($total, $num = 10, $params = ''){
			$this->total = $total;
			$this->num = $num;
			$this->cpage = !empty($_GET['page']) ? $_GET['page'] : 1;
			$this->tpage = ceil($this->total/$this->num);
			$this->uri = $this->seturi($params);
			$this->limit = $this->setlimit();
			$this->config = array('first'=>'首页', 'last'=>'尾页', 'prev'=>'上一页', 'next'=>'下一页');
			$this->fnum = 4;
			$this->limitfirst = ($this->cpage-1)*$this->num;
		}

		function __get($arg){
			if($arg=='limit'){
				return $this->limit;
			}else{
				return null;
			}
		}

		function getlimitfirst(){
			return $this->limitfirst;
		}

		private function setlimit(){
			return 'limit '.($this->cpage-1)*$this->num.' ,'.$this->num;
		}

		private function seturi($params){
			$url = $_SERVER['REQUEST_URI'].(strpos($_SERVER['REQUEST_URI'], '?') ? '' : '?').$params;
			$parseurl = parse_url($url);
			if(isset($parseurl['query'])){
				parse_str($_SERVER['query'], $parsestr);
				unset($parsestr['page']);
				$url = $parseurl['path'].'?'.http_build_query($parsestr).'&';
			}
			return $url;
		}



		private function pfirst(){
			$html = '';
			if($this->cpage<=1){
				$html.='&nbsp;'.$this->config['first'].'&nbsp;';
			}else{
				$html.='&nbsp;<a href="'.$this->uri.'page=1">'.$this->config['first'].'</a>&nbsp;';
			}
			return $html;
		}
	
		private function pprev(){
			$html = '';
			if($this->cpage<=1){
				$html.='&nbsp;'.$this->config['prev'].'&nbsp;';
			}else{
				$html.='&nbsp;<a href="'.$this->uri.'page='.($this->cpage-1).'">'.$this->config['prev'].'</a>&nbsp;';
			}
			return $html;
		}
		
		private function plist(){
			$linkpage = '';
			$num = floor($this->fnum/2);
			for($i=$num; $i>=1; $i--){
				$page = $this->cpage-$i;
				if($page<1){
					continue;
				}
				$linkpage.='&nbsp;<a href="'.$this->uri.'page='.$page.'">'.$page.'</a>&nbsp;';
			}
			$linkpage.='&nbsp;'.$this->cpage.'&nbsp;';
			for($i=1; $i<$num; $i++){
				$page = $this->cpage+$i;
				if($page>$this->tpage){
					continue;
				}
				$linkpage.='&nbsp;<a href="'.$this->uri.'page='.$page.'">'.$page.'</a>&nbsp;';
			}
			return $linkpage;
		}
	
		private function pnext(){
			$html = '';
			if($this->cpage>=$this->tpage){
				$html.='&nbsp;'.$this->config['prev'].'&nbsp;';
			}else{
				$html.='&nbsp;<a href="'.$this->uri.'page='.($this->cpage+1).'">'.$this->config['next'].'</a>&nbsp;';
			}
			return $html;
		}
	
		private function plast(){
			$html = '';
			if($this->cpage>=$this->tpage){
				$html.='&nbsp;'.$this->config['last'].'&nbsp;';
			}else{
				$html.='&nbsp;<a href="'.$this->uri.'page='.$this->tpage.'">'.$this->config['last'].'</a>&nbsp;';
			}
			return $html;
		}

		private function first(){
			return ($this->cpage-1)*$this->num+1;
		}

		private function last(){
			return min($this->cpage*$this->num, $this->total);
		}

		function fpage($option = array(0,1,2,3,4,5,6,7,8)){
			$html[] = '&nbsp;'.$this->cpage.'/'.$this->tpage.'&nbsp;';
			$html[] = '&nbsp;共'.$this->total.'条记录&nbsp;';
			$html[] = '&nbsp;第'.$this->first().'-'.$this->last().'条记录&nbsp;';
			$html[] = '&nbsp;本页有'.($this->last()-$this->first()+1).'条记录&nbsp;';
			$html[] = $this->pfirst();
			$html[] = $this->pprev();
			$html[] = $this->plist();
			$html[] = $this->pnext();
			$html[] = $this->plast();

			$page = '';
			foreach($option as $key){
				$page.=$html[$key];
			}
			return $page;
		}
	}